<?php
/**
 * Front
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library\access_control;
/**
 * Dependencies
 */
use framework\Front;
use framework\library\language;
use framework\library\access_control\Role;
use framework\library\interfaces\User_Interface;
use framework\library\authentication\Auth;
/**
 * User Class
 *
 * Authentication and Authorization for Plurious
 *
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\Front
 * @uses framework\library\language
 * @uses framework\library\access_control\Role
 * @uses framework\library\interfaces\User_Interface
 * @uses framework\library\authentication\Auth
 * 
 * @method bool isMember() Determine if user is a site member (has a role == member)
 * @method bool isEditor() Determine if user is an editor (has a role == editor)
 * @method bool isCustomer() Determine if user is a customer (has a role == customer)
 * @method bool isAdmin() Determine if user is a site administrator (has a role == admin)
 */
class User extends Role implements User_Interface {

    /** Makes the Request object available for use */
    protected $request;

    /** Makes the Document object available for use */
    protected $document;

    /** Makes the Session object available for use */
    protected $session;

    /** Makes the Config object available for use */
    protected $config;

    /** Makes the Auth object available for use */
    protected $auth;

    /** Makes the Language object available for use */
    protected $language;
    //------------------//
    /** User data: id */
    protected $id;

    /** User data: username */
    protected $username;

    /** User data: nicename */
    protected $nicename;

    /** User data: email */
    protected $email;

    /** User data: lastlogin */
    protected $lastlogin;

    /** User data: contexts this user is authorized to log in to */
    protected $contexts;

    /** User data: roles this user has on a per context basis */
    protected $roles;

    /** User data: serialized extended user data */
    protected $extended;

    /** User data: Boolean - whether the user is locked out of their account */
    protected $isLocked;

    /** User data: Boolean - whether the user's account is active or not */
    protected $isActive;

// --------------------------------------------------------------------

    /**
     * Constructor
     *
     * Loads up config variables into class variables
     *
     * @access  public
     * @return  void
     */
    public function __construct() {
        $registry = Front::getInstance();
        $this->request = $registry->get('request');
        $this->session = $registry->get('session');
        $this->config = $registry->get('config');
        $document = $registry->get('document');
        $config = $registry->get('config');
        $config->load(dirname(dirname(__FILE__)) . '/authentication/auth_config');
        $this->seedId = $config->get('seedId');
        $this->usernameCol = $config->get('UserNameCol');
        $this->passwordCol = $config->get('PasswordCol');
        $this->nicenameCol = $config->get('NiceNameCol');
        $this->rememberme = $config->get('RememberMe');
        $this->auth = new Auth();
        // Ensure the languge string can be used in the template upon rendering
        if(!is_object($this->language)) $this->language = new Language();
        // Ensure the pages don't get cached by the browser//
        $document->noCache();
        $config = null;
        $document = null;
    }

    /**
     * The preverbial log in method
     * 
     * Uses the request object to obtain cleaned form data and sends to the 
     * authentication method for processing.  Sets the session login_message 
     * value with result message from the lanugage object.
     * 
     * @return Success or Failure mode
     */
    public function login() {
        $this->username = $this->request->getPost($this->usernameCol);
        $hash = $this->request->getPost($this->passwordCol);
        $this->remember = ($this->request->getPost($this->rememberme) !== null);
        $this->session->set('remember', $this->remember);
        $this->seedId = $this->request->getPost($this->seedId);
        $result = $this->auth->authenticate($this->username, $hash);
        $resultMessage = $this->language->getText('auth_'.$result);
        $this->session->set('login_message', $resultMessage);
        
        return $resultMessage;
    }

    /**
     * Convenience method for requesting a log out
     * 
     * It's more natural for some folks to use User->logout() than Auth->logout().
     */
    public function logout() {
        $this->auth->logout();
    }

    /**
     * Convenience method for auth isAuthenticated()
     * 
     * @return type 
     */
    public function isLoggedIn() {
        return $this->auth->isAuthenticated();
    }

    /**
     * Returns the user's ID
     * 
     * @return int The user's ID
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Returns the user's usrname
     * 
     * @return string The username of the current user
     */
    public function getUserName() {
        return $this->username;
    }

    /**
     * Returns the Nice Name of the user
     * 
     * This is used as an alternative to the username for greeting purposes and
     * other personalization use cases.
     * 
     * @return string The user's Nice Name
     */
    public function getNicename() {
        return $this->nicename;
    }

    /**
     * Returns the user's e-mail address
     * 
     * @return string The user's e-mail address
     */
    public function getEmail() {
        return $this->email;
    }

    /**
     * Returns any data stored in the extended data field for this user
     * 
     * This column was added to the database for ease of use purposes. When
     * There is only one or two data points to collect beyond those listed 
     * already, it is not effiecient to build CODD Normalization!
     * 
     * @return array Associative array of extended user data
     */
    public function getExtended() {
        return unserialize($this->extended);
    }

    /**
     * Returns the last time the user logged in
     * 
     * @return string The last time the user logged in
     */
    public function getLastLogin() {
        return $this->lastlogin;
    }

    /**
     * Returns the contexts this user can log in to using a single 
     * set of credentials
     * 
     * @return array Array of contexts associated with this user
     */
    public function getContexts() {
        return $this->contexts;
    }

    /**
     * Returns the roles for all contexts set for this user
     * 
     * @return array Associative array of contexts with associated user roles
     */
    public function getRoles() {
        return $this->roles;
    }

    /**
     * Answers: Is this user account locked?
     * 
     * @return boolean True=Locked (I'll let you guess about false)
     */
    public function isLocked() {
        return $this->is_locked;
    }

    /**
     * Answers: Is this user account active?
     * 
     * @return boolean True=Active (I'll let you guess about false)
     */
    public function isActive() {
        return $this->is_active;
    }

    /**
     * A convenience method for auth getSeed()
     * 
     * The seed (nonce) used throughout the authentication process is needed
     * 
     * @return int The current seed
     */
    public function getSeed() {
        return $this->auth->getSeed();
    }

}

?>
